package protocol;

import protocol.decoder.myProtocolDecoder;
import protocol.event.DecodeEvent;
import protocol.event.DecodeEventListener;

public final class example implements DecodeEventListener, Runnable {
	private static final char[] TestData = {
		0x55, 0xAA, 0x14, 0x12, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE1,
		0x55, 0xAA, 0x14, 0x12, 0x80, 0x01, 0x00, 0x00, 0xFF, 0xF2, 0xF4, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xDA,
		0x55, 0xAA, 0x14, 0x12, 0x80, 0x01, 0x00, 0x00, 0x93, 0x23, 0xE4, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD8,
		0x55, 0xAA, 0x14, 0x12, 0x80, 0x01, 0x00, 0x01, 0xAC, 0xBE, 0xED, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1B,
		0x55, 0xAA, 0x14, 0x12, 0x80, 0x01, 0x00, 0x01, 0x93, 0x23, 0xE4, 0x3E, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58,
		0x55, 0xAA, 0x14, 0x12, 0x80, 0x01, 0x00, 0x01, 0x38, 0x8C, 0xF2, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEA,
	};

	private myProtocolDecoder decoder = null;
	public example() {
		decoder = new myProtocolDecoder();
		decoder.addDecodeListener(this);
		(new Thread(this)).start();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		int i = 0;
		do {
			try {
				decoder.rx_decode((byte) TestData[i]);
			} catch (CloneNotSupportedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			i ++;
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} while(i < 138);
		System.out.println("decode completed.");
	}

	@Override
	public void getNewPackage(DecodeEvent event) {
		// TODO Auto-generated method stub
		System.out.println("get package type: " + ((ComPackage)event.getSource()).type);
	}
	@Override
	public void badCRCEvent(DecodeEvent event) {
		// TODO Auto-generated method stub
		System.out.println("bad crc!");
	}

	public static void main(String[] args) {
		new example();
	}
}
